package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.dao.SimpleDynamicDao;
import com.xbongbong.saas.domain.entity.DynamicEntity;
import com.xbongbong.saas.domain.entity.competitor.SimpleDynamicEntity;
import com.xbongbong.saas.model.SimpleDynamicModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/11 9:22
 * @since v1.0
 */
@Service("simpleDynamicModel")
public class SimpleDynamicModelImpl implements SimpleDynamicModel {

    @Resource
    SimpleDynamicDao simpleDynamicDao;

    @Override
    public Integer insert(SimpleDynamicEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        Integer res = simpleDynamicDao.insert(entity);
        return res;
    }

    @Override
    public Integer update(SimpleDynamicEntity entity) throws XbbException {
        return null;
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return simpleDynamicDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return simpleDynamicDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public SimpleDynamicEntity getByKey(Long id, String corpid) {
        SimpleDynamicEntity entity = simpleDynamicDao.getByKey(id, corpid);
        return entity;
    }

    @Override
    public List<SimpleDynamicEntity> findEntities(Map<String, Object> param) {
        List<SimpleDynamicEntity> entities = simpleDynamicDao.findEntities(param);
        return entities;
    }

    @Override
    public void insertBatch(List<SimpleDynamicEntity> simpleDynamicEntityList) {
        if (CollectionUtils.isNotEmpty(simpleDynamicEntityList)) {
            simpleDynamicEntityList.stream().forEach(item -> checkAndParseDynamic(item));
            simpleDynamicDao.insertBatch(simpleDynamicEntityList);
        }
    }

    /**
     * 检查动态是否符合插入要求和解析处理部分参数值
     * @param simpleDynamic
     * @return
     */
    private void checkAndParseDynamic(SimpleDynamicEntity simpleDynamic) {
        if (Objects.isNull(simpleDynamic)) {
            return ;
        }
        long zero = 0L;
        Long addTime = simpleDynamic.getAddTime();
        Long updateTime = simpleDynamic.getUpdateTime();
        long now = DateUtil.getInt();
        if (Objects.isNull(addTime)) {
            simpleDynamic.setAddTime(now);
        }
        if (Objects.isNull(updateTime)) {
            simpleDynamic.setUpdateTime(now);
        }
        if (Objects.isNull(simpleDynamic.getDel())) {
            simpleDynamic.setDel(0);
        }
        // 限制memo的长度
        String memo = simpleDynamic.getMemo();
        if (StringUtil.isNotEmpty(memo)) {
            memo = memo.length() <= 255 ? memo : memo.substring(0, 252) + "...";
            simpleDynamic.setMemo(memo);
        }
        // 限制ref_name的长度
        String refName = simpleDynamic.getRefName();
        if (StringUtil.isNotEmpty(refName)) {
            refName = refName.length() <= 100 ? refName : refName.substring(0, 97) + "...";
            simpleDynamic.setRefName(refName);
        }
    }


}
